#! /bin/sh

GW_VERSION=$(cat gateway/VERSION)
GW_VERSION=${GW_VERSION#"v"}
FUSE_VERSION=$(cat sur_fuse/VERSION)
FUSE_VERSION=${FUSE_VERSION#"v"}

# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=$(readlink -f "$0")

# Absolute path this script is in, thus /home/user/bin
SCRIPTPATH=$(dirname "$SCRIPT")

DEST=$(pwd)/surcloud-gateway-$GW_VERSION/
DEBIAN=$SCRIPTPATH/surcloud-gateway.DEBIAN
rm -rfv $DEST/usr/
make -C gateway/build/ DESTDIR=$DEST install

mkdir -pv $DEST/DEBIAN/
sed "s/_VERSION_/$GW_VERSION/g" $DEBIAN > $DEST/DEBIAN/control
dpkg-deb --build $DEST


DEST=$(pwd)/surcloud-hybrid-storage-$FUSE_VERSION/
DEBIAN=$SCRIPTPATH/surcloud-hybrid-storage.DEBIAN
rm -rfv $DEST/usr/
make -C gwlib/build/ DESTDIR=$DEST install
make -C sur_fuse/build/ DESTDIR=$DEST install

mkdir -pv $DEST/DEBIAN/
sed "s/_VERSION_/$FUSE_VERSION/g" $DEBIAN > $DEST/DEBIAN/control
dpkg-deb --build $DEST
